﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mathematics
{
    public class VariableAssignment:MathExpression
    {
        public VariableAssignment(Variable variable,MathExpression assignmentExpression)
        {
            Variable = variable;
            AssignmentExpression = assignmentExpression;
        }

        public Variable Variable { get; private set; }
        public MathExpression AssignmentExpression { get; private set; }

        public override bool CanEvaluate()
        {
            return AssignmentExpression.CanEvaluate();
        }

        public override double? Evaluate()
        {
            var value = AssignmentExpression.Evaluate();
            Variable.Set(value.Value);
            return value;
        }

        protected internal override MathExpression Reduce(out bool didReduce)
        {
            var reduction = AssignmentExpression.Reduce(out didReduce);
            if (didReduce)
                return new VariableAssignment(Variable, reduction);
            return this;
        }

        //public override IEnumerable<Variable> GetVariables()
        //{
        //    yield return Variable;
        //    foreach (var v in AssignmentExpression.GetVariables())
        //        yield return v;
        //}

        //public override IEnumerable<Constant> GetConstants()
        //{
        //    return AssignmentExpression.GetConstants();
        //}
    }
}
